package pers.cz.postgirl.core.async;

import pers.cz.jefconfig.config.JefConfiguration;
import pers.cz.postgirl.config.SystemEnum;
import pers.cz.postgirl.util.JefLog;

import java.util.concurrent.*;

/**
 * @program: postgirl-client
 * @description: threadpool线程池用来处理多线程发送消息到队列
 * @author: Cheng Zhi
 * @create: 2023-05-30 15:35
 **/
public class ProductThreadPool {

    private static JefLog log = new JefLog(ProductThreadPool.class);

    private static ThreadPoolExecutor executor;

    private static void init() {

        int queueSize = JefConfiguration.getInt(SystemEnum.CLIENT_RECORE_PRODUCT_WORKQUEUECOUNT, 100);
        int corePoolSize = JefConfiguration.getInt(SystemEnum.CLIENT_RECORE_PRODUCT_WORKPOOLCORESIZE, 1);
        int maxPoolSize = JefConfiguration.getInt(SystemEnum.CLIENT_RECORE_PRODUCT_WORKPOOLMAXSIZE, 5);

        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(queueSize);
        executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, 1, TimeUnit.MILLISECONDS,
                queue);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public static ThreadPoolExecutor getExecutor() {
        if (executor == null) {
            init();
        }
        return executor;
    }
}
